package com.example.arithmeticleetcode.leetcode.october;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-10-30 15:43
 **/
public class Demo20 {

    public static void main(String[] args) {
        int[][] grid1 = {{0,1,0,0}, {1,1,1,0}, {0,1,0,0}, {1,1,0,0}};
        int[][] grid2 = {{0,1,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}};
        int[][] grid3 = {{0,1,0,0}, {0,1,0,0}, {0,0,0,0}, {0,0,0,0}};
        System.out.println(islandPerimeter(grid1));
        System.out.println(islandPerimeter(grid2));
        System.out.println(islandPerimeter(grid3));
    }

    public static int islandPerimeter(int[][] grid) {
        int oneSum = 0;
        int nextCount = 0;
        for (int i = 0, outLen = grid.length; i < outLen; i++) {
            for (int j = 0, inLen = grid[i].length; j < inLen; j++) {
                if (grid[i][j] == 1) {
                    oneSum ++;
                    if (j < inLen - 1 && grid[i][j + 1] == 1) {
                        nextCount++;
                    }
                    if (i < outLen - 1 && grid[i + 1][j] == 1) {
                        nextCount++;
                    }
                }
            }
        }
        System.out.println("总共" + oneSum + "    " + "挨着" + nextCount);
        return oneSum * 4 - nextCount* 2;
    }
}
